package com.totoro.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.totoro.domain.MySysRemarkTemplate;
import com.totoro.domain.Project;
import com.totoro.domain.PublicReply;
import com.totoro.domain.Tutor;
import com.totoro.service.MySysRemarkTemplateService;
import com.totoro.service.ProjectService;
import com.totoro.service.StaffService;

@Controller
@RequestMapping("/publicReply")
public class PublicReplyController {

    @Autowired
    private ProjectService projectService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private MySysRemarkTemplateService mySysRemarkTemplateService;
    
    private final int[] MARKS = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    
    // 答辩小组意见
    @RequestMapping("/publicReplyAdvice")
    public String publicReplyAdvice(Model model, Integer projectId, Integer tutorId) {
        
        Project project = projectService.get(projectId);
        model.addAttribute("project", project);
        
        PublicReply publicReply = project.getPublicReply();
        
        if (publicReply == null) {
            publicReply = new PublicReply();
        }
        
        model.addAttribute("publicReply", publicReply);
        model.addAttribute("marks", MARKS);
        
        Tutor tutor = new Tutor();
        tutor.setId(tutorId);
        
        List<MySysRemarkTemplate> list = getPublicReplyTemplates(tutor);
        model.addAttribute("remarkTemplateList", list);
        
        return "publicReply/publicReplyAdvice";
    }
    
    // 提交
    @RequestMapping(value = "/savePublicReply", method = RequestMethod.POST)
    public String savePublicReply(PublicReply publicReply, Integer projectId) {
        
        Project project = projectService.get(projectId);
        publicReply.setState(true);
        project.setPublicReply(publicReply);
        
        projectService.saveOrUpdate(project);
        
        return "redirect:/tutor/tutorialList.html";
    }
    
    // 导出Excel表格
    @RequestMapping("/toPublicReplyExcel")
    public String toPublicReplyExcel(Integer projectId, Model model) {
        model.addAttribute("project", projectService.get(projectId));
        return "publicReplayExcel";
    }
    
    // 暂存
    @RequestMapping("/tmpStorePublicReply")
    public String tmpStorePublicReply(PublicReply publicReply, Integer projectId) {
        
        publicReply.setState(false);
        Project project = projectService.get(projectId);
        project.setPublicReply(publicReply);
        
        projectService.saveOrUpdate(project);
        
        return "redirect:/tutor/tutorialList.html";
    }
    
    // 保存评语模板
    @RequestMapping(value = "/saveAsTemplate", method = RequestMethod.POST)
    public Object saveAsTemplate(MySysRemarkTemplate mySysRemarkTemplate, Authentication authentication, Model model) {
        Tutor tutor = staffService.uniqueResult("no", authentication.getName());
        mySysRemarkTemplate.setActor(tutor);
        mySysRemarkTemplateService.saveOrUpdate(mySysRemarkTemplate);
        
        return "mySysRemarkTemplateJson";
    }
    
    @RequestMapping(value = "/getRemarkTemplateKeywords", method = RequestMethod.POST)
    public Object getRemarkTemplateKeywords(Authentication authentication, Model model, String keyword) {
        Tutor tutor = staffService.uniqueResult("no", authentication.getName());
        List<MySysRemarkTemplate> list = getPublicReplyTemplates(tutor);
        
        boolean isDuplicated = false;
        
        for (MySysRemarkTemplate template : list) {
            if (template.getKeyword().equals(keyword)) {
                isDuplicated = true;
                break;
            }
        }
        
        model.addAttribute("isDuplicated", isDuplicated);
        
        return "remarkTemplateKeywordsJson";
    }
    
    
    // 获得答辩小组评语模板
    private List<MySysRemarkTemplate> getPublicReplyTemplates(Tutor tutor) {

        List<MySysRemarkTemplate> list = mySysRemarkTemplateService.list("actor", tutor);
        List<MySysRemarkTemplate> remarkTemplateList = new ArrayList<MySysRemarkTemplate>();
        
        // 只为用户选择含有“答辩”关键字的模板
        for (MySysRemarkTemplate template : list) {
            if (template.getKeyword().contains("答辩")) {
                remarkTemplateList.add(template);
            }
        }
        
        return remarkTemplateList;
    }
    
}
